<?php
declare(strict_types=1);

namespace ImiApp\ApiServer\Backend\Controller;

use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Phpben\Imi\Validate\Annotation\Validate;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
use Phpben\Imi\Auth\Annotation\Auth;
use Imi\Db\Db;

use ImiApp\ApiServer\Backend\Controller\CommonController as CommonController;

/**
 * @Auth(name="backend")
 * @Controller("/storeallsetlist/")
 */
class StoreallsetlistController extends CommonController
{
    /**
     * @Inject("StoreallsetlistService")
     */
    protected $service;

    /**
     * @Action
     * @Route("read")
     * @return ResponseInterface
     */
    public function read(): ResponseInterface
    {
        // $set_list = Db::name('set')->where('id','>',9)->select()->getArray();
        // // var_dump($set_list);
        // foreach ($set_list as $key => $val) {
        //     $set_id = $val['id'];
        //     unset($data);
        //     $data['set_id'] = $set_id;

        //     $res = Db::name('storeallsetlist')->whereEx($data)->find();
        //     $data['name'] = $val['name'];
        //     // var_dump($res);
        //     if($res){
        //         $data['updatetime'] = time();
        //         Db::name('storeallsetlist')->where('id','=',$res['id'])->update($data);
        //     }else{
        //        //加入
        //        $data['createtime'] = time();
        //        $data['updatetime'] = time();
        //     //    var_dump($data);
        //        Db::name('storeallsetlist')->insert($data);
        //     }
        // }
        return $this->response->success(null, $this->service->read() ?: []);
    }

    /**
     * @Action
     * @Route(url="create",method="POST")
     * @Validate
     * @param $data
     * @return ResponseInterface
     */
    public function create($data): ResponseInterface
    {
        return $this->service->create($data) ? $this->response->success('创建成功') : $this->response->error($this->service->getError());
    }

    /**
     * @Action
     * @Route(url="update",method="POST")
     * @Validate
     * @param $data
     * @return ResponseInterface
     */
    public function update($data): ResponseInterface
    {
        return $this->service->update($this->request->getParsedBody()) ? $this->response->success('更新成功') : $this->response->error($this->service->getError());
    }

    /**
     * @Action
     * @Route(url="delete",method="POST")
     * @return ResponseInterface
     */
    public function delete(): ResponseInterface
    {
        return $this->service->delete() ? $this->response->success('删除成功') : $this->response->error($this->service->getError());
    }

    

        
}